//var urlcore = 'https://app.jsbaitu.com/interface';
//var urlcore = 'http://app.xmrxzl.com/interface';
var urlcore = 'http://localhost:8084';


//设置全局ajax
$.ajaxSetup({
    xhrFields: {
    	withCredentials: true
	}
});

//设置分页 每页数据条数
var size=10;

function getvl(name) {
	var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
	if(reg.test(location.href.trim())) return unescape(RegExp.$2.replace(/\+/g, " "));
	return "";
};


/*//从url中获取参数值
function getvl(name) {
       // 获取参数
    var url = window.location.search;
    // 正则筛选地址栏
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    // 匹配目标参数
    var result = url.substr(1).match(reg);
    //返回参数值
    return result ? decodeURIComponent(result[2]) : "";
};
*/



 //初始化fileinput控件（第一次初始化）
function initFileInput(ctrlName, uploadUrl,okName,maxnNum) {    
    var control = $('#' + ctrlName); 
    control.fileinput({
        language: 'zh', //设置语言
        uploadUrl: uploadUrl, //上传的地址
        //uploadExtraData:{"id": 1, "fileName":'123.mp3'},
        uploadAsync: true, //默认异步上传
        showUpload: true, //是否显示上传按钮
        showRemove : true, //显示移除按钮
        showPreview : true, //是否显示预览
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式     
        dropZoneEnabled: true,//是否显示拖拽区域
        maxFileCount: 10, //表示允许同时上传的最大文件个数
        dropZoneTitle: '拖拽文件到这里 &hellip;',
        maxFileSize:0,
        enctype: 'multipart/form-data',
        validateInitialCount:true,
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
    }).on("fileuploaded", function(event, data,key) {
        if(data.response){	
        	var okData = data.response.msg;
//      	for (var i = 0; i < okData.length; i++) {
        		var strHtml = 
		            	'<div style="position:relative;width:250px;height:250px;float:left;display:none" id="'+key+'">'+
		            	'	<span onclick="removeStrHtml()" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">'+
		            	'	</span>'+
		            	'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="'+okData+'">'+
		        		'</div>';
		        $(okName).append(strHtml);
//      	}
        }
    }).on('filesuccessremove', function(event, key) { 
    	$("#"+key).remove();  
	});
}



/**
 * 移除Html代码
 */
function removeStrHtml(obj){
	$(obj).parent().remove();
}


//删除数组元素
function removeByValue(arr, val) {
  for(var i=0; i<arr.length; i++) {
    if(arr[i] == val) {
      arr.splice(i, 1);
      break;
    }
  }
}

/**
 * 时间日期控件
 */
function timeControl(idhtml){
// 初始化日期范围插件
var thisid='#'+idhtml;
	$(thisid).daterangepicker({
		"showDropdowns": true,
		"autoApply": false,
		// "dateLimit": {
		//     "days": 365 * 100
		// },
		"linkedCalendars": false,
		"opens": 'right',
		"ranges": {
			"一周内": [
				moment().subtract(6, 'days'),
				moment()
			],
			"一月内": [
				moment().subtract(29, 'days'),
				moment()
			],
			"本月内": [
				moment().startOf('month'),
				moment().endOf('month')
			],
			"上个月": [
				moment().subtract(1, 'month').startOf('month'),
				moment().subtract(1, 'month').endOf('month')
			]
		},
		autoUpdateInput: false,
		locale: {
			format: 'YYYY-MM-DD',
			separator: ' - ',
			applyLabel: '确定',
			cancelLabel: '取消',
			fromLabel: '从',
			toLabel: '到',
			customRangeLabel: '日期范围',
			daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
			monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
			firstDay: 1
		},
		"minDate": "YYYY-MM-DD",
		"maxDate": "YYYY-MM-DD"
	}, function(start, end, label) {
		console.log("New date range selected: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + " (predefined range: " + label + ")");
	});

	$(thisid).on('apply.daterangepicker', function(ev, picker) {
		$(this).val(picker.startDate.format('YYYY-MM-DD') + '~' + picker.endDate.format('YYYY-MM-DD'));
	});

	$(thisid).on('cancel.daterangepicker', function(ev, picker) {
		$(this).val('');
	});
}


 // base64加密开始  
    var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv"  
            + "wxyz0123456789+/" + "=";  

/**
 * 加密
 */
function encode64(input) {  
	  
    var output = "";  
	var chr1, chr2, chr3 = "";  
	var enc1, enc2, enc3, enc4 = "";  
	var i = 0;  
do {  
    chr1 = input.charCodeAt(i++);  
    chr2 = input.charCodeAt(i++);  
    chr3 = input.charCodeAt(i++);  
    enc1 = chr1 >> 2;  
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);  
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);  
    enc4 = chr3 & 63;  
    if (isNaN(chr2)) {  
        enc3 = enc4 = 64;  
    } else if (isNaN(chr3)) {  
        enc4 = 64;  
    }  
    output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)  
            + keyStr.charAt(enc3) + keyStr.charAt(enc4);  
    chr1 = chr2 = chr3 = "";  
    enc1 = enc2 = enc3 = enc4 = "";  
        } while (i < input.length);  
		  
        return output;  
 }  
   	 


Date.prototype.pattern=function(fmt) {         
    var o = {         
    "M+" : this.getMonth()+1, //月份         
    "d+" : this.getDate(), //日         
    "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时         
    "H+" : this.getHours(), //小时         
    "m+" : this.getMinutes(), //分         
    "s+" : this.getSeconds(), //秒         
    "q+" : Math.floor((this.getMonth()+3)/3), //季度         
    "S" : this.getMilliseconds() //毫秒         
    };         
    var week = {         
    "0" : "/u65e5",         
    "1" : "/u4e00",         
    "2" : "/u4e8c",         
    "3" : "/u4e09",         
    "4" : "/u56db",         
    "5" : "/u4e94",         
    "6" : "/u516d"        
    };         
    if(/(y+)/.test(fmt)){         
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));         
    }         
    if(/(E+)/.test(fmt)){         
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);         
    }         
    for(var k in o){         
        if(new RegExp("("+ k +")").test(fmt)){         
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));         
        }         
    }         
    return fmt;         
}       
     
//获取当前月的第一天

function getCurrentMonthFirst(){
 var date=new Date();
 date.setDate(1);
 return date.pattern("yyyy-MM-dd");
}

// 获取当前月的最后一天
function getCurrentMonthLast(){
 var date=new Date();
 var currentMonth=date.getMonth();
 var nextMonth=++currentMonth;
 var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
 var oneDay=1000*60*60*24;
 return new Date(nextMonthFirstDay-oneDay).pattern("yyyy-MM-dd");
}


//我的权限数组    
var arrayTitle = new Array; 
findMyCatalogue(getCookie('Jname'));
//权限展示方法
function findMyCatalogue(jName){
	$.ajax({
		url: urlcore + "/api/roleThirdCatalogue/findAllByUser?secondTitle="+jName,
		type: "GET",
		dataType: 'json',
		async: false,
		contentType: "application/json;charset=utf-8",
		success:function(data){
		if (data.success == true) {
			$.each(data.data, function(i,n) {
				arrayTitle.push(n.thirdCatalogue.title);
			});
		} else {
//			alert(data.msg);
		}
		},
		error:function() {
			
		}
	});
}

function buttonShow(){
	$.each(arrayTitle, function(i,k) {
		$('a[name="'+k+'"]').attr("hidden",false).attr("class","btn btn-primary btn-xs");
		$('a[data="'+k+'"]').attr("class","btn btn-sm btn-primary");
	});
}

//设置cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);

//document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();

document.cookie = name + "="+ escape (value);
}

//获取cookies
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}

//删除cookies
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
